package com.dmgis.qxfw.dataservice.businessservice.yyhb.service.Impl;

import com.dmgis.entity.DmMapPoint;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataByDelete;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataByInsert;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.*;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.ActualElemQuery;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.ActualData;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.TownshipForecast;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.TownshipForecastKey;
import com.dmgis.qxfw.dataservice.businessservice.common.pojo.HighAltitude;
import com.dmgis.qxfw.dataservice.businessservice.common.pojo.MixHeightDay;
import com.dmgis.qxfw.dataservice.businessservice.common.pojo.SpecialServiceStation;
import com.dmgis.qxfw.dataservice.businessservice.common.util.*;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.yyhb.service.ForecastProductServiceI;
import com.dmgis.qxfw.dataservice.gisservice.service.ITransformService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ForecastProtectServiceImpl implements ForecastProductServiceI {

    ProcessRequestController procon=new ProcessRequestController();

    @Resource
    ITransformService iTransformService;

    @Override
    public List<SpecialServiceStation> getSpecial() {
        List<special_srv_station> listData = new ArrayList<special_srv_station>();
        List<SpecialServiceStation> resultList = new ArrayList<SpecialServiceStation>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("stationid");
        columns.add("stationname");
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        strWhere.append(String.format(" and flag='%s'", "高空"));
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        try {
            selectParam.put("method", "special_srv_station");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<special_srv_station>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
        }
        if (listData != null && listData.size() > 0) {
            for (special_srv_station specialstation : listData) {
                SpecialServiceStation ssstation = new SpecialServiceStation();
                ssstation.setStationId(specialstation.getStationid());
                ssstation.setStationName(specialstation.getStationname());
                resultList.add(ssstation);
            }
        }
        return resultList;
    }

    @Override
    public List<HighAltitude> getHighAltitude(String date) {
        List<high_elements> listData = new ArrayList<high_elements>();
        List<HighAltitude> resultList = new ArrayList<HighAltitude>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("drybulbtemp");
        columns.add("stationpress");
        columns.add("stationid");
        columns.add("stationname");
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        // 拼接where字段
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (date != null) {
            strWhere.append(String.format(" and observtime='%s'", date));
        }
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        try {
            selectParam.put("method", "high_elements");
            selectParam.put("sqlType", "select");
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<high_elements>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
        }
        if (listData != null && listData.size() > 0) {
            for (high_elements highelements : listData) {
                HighAltitude highaltitude = new HighAltitude();
                highaltitude.setDryBulbTemp(highelements.getDrybulbtemp());
                highaltitude.setStationPress(highelements.getStationpress());
                highaltitude.setStationID(highelements.getStationid());
                highaltitude.setStationName(highelements.getStationname());
                resultList.add(highaltitude);
            }
        }
        return resultList;
    }

    @Override
    public List<HighAltitude> getGroundElements(String date) {
        List<ground_elements> listData = new ArrayList<ground_elements>();
        List<HighAltitude> resultList = new ArrayList<HighAltitude>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("drybulbtemp");
        columns.add("stationid");
        columns.add("stationpress");
        columns.add("stationname");
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        // 拼接where字段
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (date != null) {
            strWhere.append(String.format(" and observtime='%s'", date));
        }
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        try {
            selectParam.put("method", "ground_elements");
            selectParam.put("sqlType", "select");
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message =procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<ground_elements>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
        }
        if (listData != null && listData.size() > 0) {
            for (ground_elements groundelements : listData) {
                HighAltitude highaltitude = new HighAltitude();
                highaltitude.setDryBulbTemp(groundelements.getDrybulbtemp());
                highaltitude.setStationID(groundelements.getStationid());
                highaltitude.setStationName(groundelements.getStationname());
                highaltitude.setStationPress(groundelements.getStationpress());
                resultList.add(highaltitude);
            }
        }
        return resultList;
    }

    @Override
    public void delTempInver(String date) {
        Map<String, String> deleteParam = new HashMap<String, String>();
        RequestDataByDelete<String> deleteRequest = new RequestDataByDelete<String>();
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (date != null) {
            strWhere.append(String.format(" and datechar='%s'", date));
        }
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        deleteRequest.setParam(strWhere.toString());
        try {
            deleteParam.put("method", "temp_inversion");
            deleteParam.put("sqlType", "delete");
            deleteParam.put("advCode", DbConfig.advCode);
            deleteParam.put("param", JsonUtil.object2Json(deleteRequest));
            String message = procon.processRequest(deleteParam);
            System.out.println(message);
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("error!");
        }
    }

    @Override
    public void insertTempInver(String strstaName, String date, String timeChar, String s) {
        Map<String, String> insertParam = new HashMap<String, String>();
        RequestDataByInsert<temp_inversion> insertRequest = new RequestDataByInsert<temp_inversion>();

        List<temp_inversion> insertListData = new ArrayList<temp_inversion>();
        temp_inversion insertData = new temp_inversion();
        insertData.setStationname(strstaName);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            insertData.setDatechar(sdf.parse(date));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        insertData.setTimechar(timeChar);
        insertData.setTempinversion(Integer.parseInt(s));
        insertListData.add(insertData);
        insertRequest.setData(insertListData);
        insertParam.put("method", "temp_inversion");
        insertParam.put("advCode", DbConfig.advCode);
        insertParam.put("sqlType", "insert");
        try {
            insertParam.put("param", JsonUtil.object2Json(insertRequest));
        } catch (JsonProcessingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String message1 = procon.processRequest(insertParam);
        System.out.println(message1);
    }

    @Override
    public List<MixHeightDay> getMixHeightDay(String date, String date1, String radio) {
        List<mixing_height> listData = new ArrayList<mixing_height>();
        List<MixHeightDay> resultList = new ArrayList<MixHeightDay>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("(cast(datechar as timestamp)+ cast(ntimes||'hour' as INTERVAL)) as datechar");
        columns.add("mixingheight");
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        selectRequest.setSort(new String[]{"(cast(datechar as timestamp)+ cast(ntimes||'hour' as INTERVAL)) asc"});
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (date != null) {
            strWhere.append(String.format(" and (cast(datechar as timestamp)+ cast(ntimes||'hour' as INTERVAL)) >='%s'", date));
        }
        if (date1 != null) {
            strWhere.append(String.format(" and (cast(datechar as timestamp)+ cast(ntimes||'hour' as INTERVAL)) <='%s'", date1));
        }
        if ("时".equals(radio)) {
            strWhere.append(String.format(" and forecasttype='%s'", "1"));//24
        } else {
            strWhere.append(String.format(" and forecasttype='%s'", "24"));//24
        }
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        try {
            selectParam.put("method", "mixing_height");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<mixing_height>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
        }
        if (listData != null && listData.size() > 0) {
            for (mixing_height mixheight : listData) {
                MixHeightDay mixheightday = new MixHeightDay();
                mixheightday.setDateChar(mixheight.getDatechar());
                mixheightday.setMixingHeightDay(mixheight.getMixingheight().floatValue());
                resultList.add(mixheightday);
            }
        }
        return resultList;
    }

    @Override
    public List<township_station> getMetStation() {
        List<township_station> listData = new ArrayList<township_station>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("stationid");
        columns.add("stationname");
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        try {
            selectParam.put("method", "township_station");
            selectParam.put("sqlType", "select");
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<township_station>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
        }
        return listData;
    }

    @Override
    public List<township_forecasttemp> getMetElementInfo(String dateChar,String ntime,String station) {
        List<township_forecasttemp> listData=new ArrayList<township_forecasttemp>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("stationid");
        columns.add("timechar");
        columns.add("ntimes");
        columns.add("to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar");
        columns.add("StationName");
        columns.add("longitude");
        columns.add("latitude");
        columns.add("altitude");
        columns.add("windd");
        columns.add("winds");
        columns.add("rain");
        columns.add("cloud");
        columns.add("maxtemp");
        columns.add("mintemp");
        columns.add("humid");
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        selectRequest.setSort(new String[] { "cast(Ntimes as numeric(18,1)) asc"});
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (dateChar!=null) {
            strWhere.append(String.format(" and datechar='%s'",dateChar));
        }
        if (ntime!=null) {
            strWhere.append(String.format(" and timechar='%s'",ntime));
        }
        if (station!=null) {
            strWhere.append(String.format(" and stationname in %s",station));
        }
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        try {
            selectParam.put("method", "township_forecast");
            selectParam.put("sqlType", "select");
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<township_forecasttemp>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
        }
        return listData;
    }

    @Override
    public Date tempInverInitTime() {
        Date date = new Date();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        String[] columns = new String[]{"to_char(observtime, 'yyyy-MM-dd HH24:MI:SS') as observtime"};
        selectRequest.setColumns(columns);
        selectRequest.setSort(new String[]{"observtime desc"});
        selectRequest.setLimit(1);
        try {
            selectParam.put("method", "high_elements");
            selectParam.put("sqlType", "select");
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<high_elements>>() {
            }).get(0).getObservtime();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return date;
    }

    @Override
    public Date mixHeightInitTime() {
        Date date = new Date();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        String[] columns = new String[]{"to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar"};
        selectRequest.setColumns(columns);
        selectRequest.setSort(new String[]{"datechar desc"});
        selectRequest.setLimit(1);
        try {
            selectParam.put("method", "mixing_height");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message =procon.processRequest(selectParam);
            date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<mixing_height>>() {
            }).get(0).getDatechar();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return date;
    }

    @Override
    public township_forecasttemp metEleInitTime() {
        township_forecasttemp township = new township_forecasttemp();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        String[] columns = new String[]{"to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar", "timechar"};
        selectRequest.setColumns(columns);
        selectRequest.setSort(new String[]{"datechar desc", "timechar desc"});
        selectRequest.setLimit(1);
        try {
            selectParam.put("method", "township_forecasttemp");
            selectParam.put("sqlType", "select");
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            township = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<township_forecasttemp>>() {
            }).get(0);
        } catch (Exception e) {
            // TODO: handle exception
            township.setDatechar(DateTime.now().toDate());
            township.setNtimes(DateTime.now().GetHour() >= 8 && DateTime.now().GetHour() < 20 ? 8 : 20);
        }
        return township;
    }

    @Override
    public List<MixHeightDay> getMixHeightSK(String dateTime, String dateTime1) {
        // TODO Auto-generated method stub
        List<mixing_heightsk> listData = new ArrayList<mixing_heightsk>();
        List<MixHeightDay> resultList = new ArrayList<MixHeightDay>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("to_char(observtime, 'yyyy-MM-dd HH24:MI:SS') as observtime");
        columns.add("mixingheight");
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        selectRequest.setSort(new String[]{"observtime asc"});
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (dateTime != null) {
            strWhere.append(String.format(" and observtime>='%s'", dateTime));
        }
        if (dateTime1 != null) {
            strWhere.append(String.format(" and observtime<='%s'", dateTime1));
        }
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        try {
            selectParam.put("method", "mixing_heightsk");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<mixing_heightsk>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
        }
        if (listData != null && listData.size() > 0) {
            for (mixing_heightsk mixheight : listData) {
                MixHeightDay mixheightday = new MixHeightDay();
                mixheightday.setDateChar(mixheight.getObservtime());
                mixheightday.setMixingHeightDay(mixheight.getMixingheight().floatValue());
                resultList.add(mixheightday);
            }
        }
        return resultList;
    }

    @Override
    public List<temp_inversionyb> getYBTempInversion(String startTime, String timeChar, String hourorday) {
        // TODO 逆温预报
        List<temp_inversionyb> listData = new ArrayList<temp_inversionyb>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("stationname");
        columns.add("type");
        columns.add("(cast(datechar as timestamp)+ cast(timechar||'hour' as INTERVAL)+ cast(ntimes||'hour' as INTERVAL)) as datechar");
        columns.add("tempinversion");
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        selectRequest.setSort(new String[]{"(cast(datechar as timestamp)+ cast(timechar||'hour' as INTERVAL)+ cast(ntimes||'hour' as INTERVAL)) asc"});
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        if (!StringUtil.IsNullOrEmpty(startTime)) {
            strWhere.append(String.format(" and (cast(datechar as timestamp)+ cast(timechar||'hour' as INTERVAL)+ cast(ntimes||'hour' as INTERVAL))>='%s'", startTime));
        }
        if (!StringUtil.IsNullOrEmpty(timeChar)) {
            strWhere.append(String.format(" and timechar='%s'", timeChar));
        }
        if (hourorday.equals("hour")) {
            strWhere.append(String.format(" and ntimes<='24'"));
            strWhere.append(String.format(" and fcsttype='1'"));
        } else {
            strWhere.append(String.format(" and ntimes in ('012','024','036','048','060','072','084','096','108','120','132','144','156','168')"));
            strWhere.append(String.format(" and fcsttype='12'"));
        }
        if (!StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        try {
            selectParam.put("method", "temp_inversionyb");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode", DbConfig.advCode);
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<temp_inversionyb>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
        }
        return listData;
    }

    @Override
    public Date tempInverYBInitTime() {
        // TODO 逆温预报初始化时间
        Date date = new Date();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        String[] columns = new String[]{"to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar"};
        selectRequest.setColumns(columns);
        selectRequest.setSort(new String[]{"datechar desc"});
        selectRequest.setLimit(1);
        try {
            selectParam.put("method", "temp_inversionyb");
            selectParam.put("sqlType", "select");
            selectParam.put("advCode",DbConfig.advCode );
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<temp_inversionyb>>() {
            }).get(0).getDatechar();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return date;
    }



    @Override
    public Date initHighelements() {
        Date date=new Date();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        String[] columns = new String[] { "to_char(observtime, 'yyyy-MM-dd HH24:MI:SS') as observtime"};
        selectRequest.setColumns(columns);
        selectRequest.setSort(new String[] { "observtime desc"});
        selectRequest.setLimit(1);
        try {
            selectParam.put("method", "high_elements");
            selectParam.put("sqlType", "select");
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<high_elements>>() {
            }).get(0).getObservtime();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return date;
    }

    @Override
    public List<high_elements> GetHighelements(String date,String press) {
        List<high_elements> elementslist = new ArrayList<>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("stationid");
        columns.add("stationname");
        columns.add("to_char(observtime, 'yyyy-MM-dd HH24:MI:SS') as observtime");
        columns.add("longitude");
        columns.add("latitude");
        columns.add("altitude");
        columns.add("city");
        columns.add("county");
        columns.add("stationpress");
        columns.add("drybulbtemp");
        columns.add("dewpointtemp");
        columns.add("winddirect");
        columns.add("windvelocity");
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        selectRequest.setParam(String.format(" observtime='%s' and stationpress='%s'",date,press));
        try {
            selectParam.put("method", "high_elements");
            selectParam.put("sqlType", "select");
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            elementslist = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<high_elements>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
        }
        return elementslist;



    }


    @Override
    public List<ActualData> qxElementQuery(ActualElemQuery elemQuery, String pree) {
        // TODO 气象要素统计
        List<ActualData> resultList = new ArrayList<>();
        List<high_elements> dataList = new ArrayList<>();
        Map<String, String> selectParam = new HashMap<>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        String element = elemQuery.getElement();
        String type = elemQuery.getType();
        Date startDate = elemQuery.getStartDate();
        Date endDate = elemQuery.getEndDate();
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;
        if (startDate != null) {
            whereCount++;
            strWhere.append(String.format("observTime >='%tF %tT'", startDate, startDate));
        }
        if (endDate != null) {
            strWhere.append(String.format(" and observTime <='%tF %tT'", endDate, endDate));
        }
        strWhere.append(String.format(" and stationpress='%s'", pree));
        // 获取查询字段
        List<String> columns = new ArrayList<String>();
        columns.add("stationid");// pg字段大小写不敏感的话，默认全部转换成小写模式
        columns.add("stationname");
        columns.add("longitude");
        columns.add("latitude");
        if (element != null && element.equals("temp")) {
            // 温度统计
            if (type != null && type.equals("sum") || type.equals("avg") || type.equals("min") || type.equals("max")) {
                columns.add("cast(" + type + "(cast(drybulbtemp as numeric(18,1))) as FLOAT) as drybulbtemp");
                strWhere.append(" and drybulbtemp <> -65535");
                selectRequest.setSort(new String[]{"drybulbtemp desc"});
            }
            // 表格统计
            if (type != null && (type.equals("spline") || type.equals("column"))) {
                if (elemQuery.getDaytype() != null && elemQuery.getDaytype().contains("日均")) {
                    columns.add("to_char(observtime,'yyyy-MM-dd') as time");
                    columns.add("avg(drybulbtemp) as drybulbtemp");
                    strWhere.append(" and drybulbtemp <> -65535");
                    selectRequest.setSort(new String[]{"to_char(observtime,'yyyy-MM-dd')"});
                } else {
                    columns.add("observtime");
                    columns.add("drybulbtemp");
                    strWhere.append(" and drybulbtemp <> -65535");
                    selectRequest.setSort(new String[]{"observtime"});
                }
            }
        }
        if (element != null && element.equals("winds")) {
            // 风速统计
            if (type != null && type.equals("sum") || type.equals("avg") || type.equals("min") || type.equals("max")) {
                columns.add(type + "(windvelocity) as windvelocity");
                columns.add("winddirect");
                strWhere.append(" and windvelocity <> -65535");
                strWhere.append(" and winddirect <> -65535");
                selectRequest.setSort(new String[]{"windvelocity desc"});
            }
            // 表格统计
            if (type != null && (type.equals("spline") || type.equals("column"))) {
                if (elemQuery.getDaytype() != null && elemQuery.getDaytype().contains("日均")) {
                    columns.add("to_char(observtime,'yyyy-MM-dd') as time");
                    columns.add("avg(windvelocity) as windvelocity");
                    strWhere.append(" and windvelocity <> -65535");
                    selectRequest.setSort(new String[]{"to_char(observtime,'yyyy-MM-dd')"});
                } else {
                    columns.add("observtime");
                    columns.add("windvelocity");
                    strWhere.append(" and windvelocity <> -65535");
                    selectRequest.setSort(new String[]{"observtime"});
                }
            }
        }
        if (element != null && element.equals("pressure")) {
            // 气压统计
            if (type != null && type.equals("sum") || type.equals("avg") || type.equals("min") || type.equals("max")) {
                columns.add(type + "(stationpress) as stationpress");
                strWhere.append(" and stationpress <> -65535");
                selectRequest.setSort(new String[]{"stationpress desc"});
            }
            // 表格统计
            if (type != null && (type.equals("spline") || type.equals("column"))) {
                if (elemQuery.getDaytype() != null && elemQuery.getDaytype().contains("日均")) {
                    columns.add("to_char(observtime,'yyyy-MM-dd') as time");
                    columns.add("avg(stationpress) as StationPress");
                    strWhere.append(" and stationpress <> -65535");
                    selectRequest.setSort(new String[]{"to_char(observtime,'yyyy-MM-dd')"});
                } else {
                    columns.add("observtime");
                    columns.add("stationpress");
                    strWhere.append(" and stationpress <> -65535");
                    selectRequest.setSort(new String[]{"observtime"});
                }
            }
        }
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        try {
            selectParam.put("method", "high_elements");
            selectParam.put("sqlType", "select");
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            dataList = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<high_elements>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
            System.err.println(e.getStackTrace());
        }
        // 赋值
        try {
            WebCtrlData webCtrlData = new WebCtrlData();
            if (dataList.size() > 0) {
                for (high_elements data : dataList) {
                    ActualData actualData = new ActualData();
                    actualData.setStationId(data.getStationid().trim());
                    actualData.setStationName(data.getStationname().trim());
                    actualData.setLon(data.getLongitude());
                    actualData.setLat(data.getLatitude());
                    DmMapPoint dt = iTransformService.LonLatConvertToLambert(data.getLongitude(), data.getLatitude());
                    actualData.setX(dt.x);
                    actualData.setY(dt.y);
                    switch (element) {
                        case "temp":
                            actualData.setTemp(Double.parseDouble(String.format("%.1f", data.getDrybulbtemp())));
                            break;
                        case "winds":
                            actualData.setWinds(Double.parseDouble(String.format("%.1f", data.getWindvelocity())));
                            if (type != null && type.equals("sum") || type.equals("avg") || type.equals("min") || type.equals("max")) {
                                String s = webCtrlData.GetAllFx(data.getWinddirect().toString());
                                actualData.setWinddirect(s);
                                int i = webCtrlData.FengSuToFengJi(data.getWindvelocity());
                                actualData.setWindvelocity(i);
                            }
                            break;
                        case "pressure":
                            actualData.setPressure(Double.parseDouble(String.format("%.1f", data.getStationpress())));
                            break;
                        default:
                            break;
                    }
                    if (elemQuery.getDaytype() != null && elemQuery.getDaytype().contains("日均")) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        actualData.setDateTime(sdf.parse(data.getTime()));
                    } else {
                        actualData.setDateTime(data.getObservtime());
                    }
                    resultList.add(actualData);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }
        return resultList;
    }


    @Override
    public Date InitWeatherImagetime() {
        weatherchart newDate = new weatherchart();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        String[] columns = new String[]{"to_char(observtime, 'yyyy-MM-dd HH24:MI:SS') as observtime"};
        selectRequest.setColumns(columns);
        selectRequest.setSort(new String[]{"observtime desc"});
        selectRequest.setLimit(1);
        try {
            selectParam.put("method", "weatherchart");
            selectParam.put("sqlType", "select");
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message =procon.processRequest(selectParam);
            newDate = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<weatherchart>>() {
            }).get(0);
        } catch (Exception e) {
            // TODO: handle exception
            newDate.setObservtime(DateTime.now().toDate());
        }
        return newDate.getObservtime();
    }



    @Override
    public List<weatherchart> showWeatherImg(String startDate, String endDate,String level,String type) {
        List<weatherchart> base_products = new ArrayList<weatherchart>();
        weatherchart newDate = new weatherchart();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        if (startDate != null) {
            strWhere.append(String.format("observtime>='%s'",startDate));
        }
        if (endDate != null) {
            strWhere.append(String.format(" and observtime<='%s'",endDate));
        }
        if (level != null) {
            strWhere.append(String.format(" and arrangement='%s'",level));
        }
        if (type != null) {
            strWhere.append(String.format(" and type='%s'",type));
        }
        selectRequest.setParam(strWhere.toString());
        selectRequest.setSort(new String[]{"observtime asc"});
        try {
            selectParam.put("method", "weatherchart");
            selectParam.put("sqlType", "select");
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            base_products = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<weatherchart>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
            newDate.setObservtime(DateTime.now().toDate());
        }
        return base_products;
    }

    @Override
    public List<TownshipForecast> getfcstPoint(TownshipForecastKey newestDate, String[] station) {
        List<TownshipForecast> resultList = new ArrayList<TownshipForecast>();
        Map<String, String> selectParam = new HashMap<String, String>();
        RequestDataBySelect selectRequest = new RequestDataBySelect();
        List<String> columns = new ArrayList<String>();
        columns.add("StationName");
        columns.add("CAST(replace(cast(humid AS VARCHAR),'-65535','99999') as numeric(18,1)) as humid");
        columns.add("to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as dateChar");
        columns.add("NTimes");
        columns.add("CAST(replace(cast(maxtemp AS VARCHAR),'-65535','99999') as numeric(18,1)) as maxtemp");
        columns.add("CAST(replace(cast(minTemp AS VARCHAR),'-65535','99999') as numeric(18,1)) as minTemp");
        columns.add("winds");
        columns.add("rain");
        selectRequest.setColumns(columns.toArray(new String[columns.size()]));
        // 拼接where字段
        StringBuilder strWhere = new StringBuilder();
        int whereCount = 0;

        if (newestDate.getDateChar() != null) {
            strWhere.append(String.format(" and datechar='%tF'", newestDate.getDateChar()));
        }
        if (!StringUtil.IsNullOrEmpty(newestDate.getTimeChar())) {
            strWhere.append(String.format(" and timechar='%s'", newestDate.getTimeChar()));
        }
        StringBuilder st=new StringBuilder();
        st.append("(");
        for(String s:station){
            st.append("'"+s+"',");
        }
        String sta=st.substring(0,st.length()-1);
        sta+=")";
        strWhere.append(" and stationid='"+DbConfig.STATIONID+"'");
        strWhere.append(" and ntimes%24=0");
        if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
            strWhere = strWhere.replace(0, 4, "");
        }
        selectRequest.setParam(strWhere.toString());
        selectRequest.setSort(new String[]{"datechar asc", "NTimes ASC","Stationname ASC"});
        try {
            selectParam.put("method", "township_forecasttemp");
            selectParam.put("sqlType", "select");
            selectParam.put("param", JsonUtil.object2Json(selectRequest));
            String message = procon.processRequest(selectParam);
            resultList = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<TownshipForecast>>() {
            });
        } catch (Exception e) {
            // TODO: handle exception
            System.err.println(e.getStackTrace());
        }
        return resultList;
    }


}
